<template>
	<el-container>
		<el-header>
			<div class="left-panel">
				<fsButton ref="button" plain size="default" :buttons="buttons" @clickHandler="buttonHandler"></fsButton>
			</div>
			<div class="right-panel">
				<ex-search ref="search" :searches="searches" @searchOk="searchHandler" default-code="code" placeholder="请输入单据编码" :fixed-param="{manageDeptIds:this.manageDeptIds,manageAssetCategoryIds:this.manageAssetCategoryIds}"></ex-search>
			</div>
		</el-header>
		<el-main class="nopadding">
			<ex-table id="checkTaskTable" ref="table" :tablename="'单据明细'" :columns="columns" :url="this.$API.asset.documentDetail.customPage.url" :refs="this.$refs" search="search" @columnClick="columnClickHandler"></ex-table>
		</el-main>
	</el-container>

	<data-card ref="dataCard"></data-card>
</template>

<script>
import ExTable from "@/components/exTable/index.vue";
import ExSearch from "@/components/exSearch/index.vue";
import ExTreeNavigation from "@/components/exSearchTree/index.vue";
import ExSearchTree from "@/components/exSearchTree/index.vue";
import scSelectFilter from "@/components/scSelectFilter/index.vue";
import tool from "@/utils/tool";
import DataCard from "@/views/asset/card/card.vue";
import BaseTemplate from "@/utils/baseTemplate";
export default {
	name: '单据明细',
	components: {
		DataCard,
		scSelectFilter,
		ExSearchTree,
		ExTreeNavigation,
		ExSearch,
		ExTable,
	},
	data() {
		return {
			dialog: {
				save: false,
				permission: false,
			},
			apiObj: [],
			buttons: [
				{name: "导出", icon: "el-icon-download", click: "export", type: "info", permission: "export"},
			],
			searches: [
				{type: "tree-select", name: "单据类型", code: "documentConfigIds", placeholder: "请选择单据类型", dictCode:'FamsDocumentConfig',checkStrictly:true,multiple:true,dataPermissionProcess:true},
				{type: "input", name: "单据编码", code: "code", placeholder: "请输入编码", maxlength: 30, showLimit: true},
				{type: "input", name: "品牌", code: "assetInfo.brand", placeholder: "请输入品牌"},
				{type: "input", name: "规格型号", code: "assetInfo.model", placeholder: "请输入规格型号"},
				{type: "input", name: "序列号", code: "assetInfo.serialCode", placeholder: "请输入序列号"},
				{type: "input", name: "资产名称", code: "assetInfo.name", placeholder: "请输入资产名称"},
				{type: "daterange", name: "制单日期", code: "makeDateStart,makeDateEnd", placeholder: "请选择制单日期", formatter:"YYYY-MM-dd",valueFormat:"YYYYMMDD",range:365},
				{type: "selector", name: "制单人", code: "createUser", placeholder: "请选择制单人", dictCode:'SysUser',priKey:true},
				{type: "tree-select", name: "制单部门", code: "createOrganizationIds", placeholder: "请选择制单部门", dictCode:'SysOrganization',priKey:true,checkStrictly:true,multiple:true,dataPermissionProcess:true,permissionData:this.$TOOL.current.readDeptIds()},
				{type: "tree-select", name: "资产类别", code: "assetCategorys", placeholder: "请选择资产类别", dictCode:'FamsAssetCategory',checkStrictly:true,multiple:true,dataPermissionProcess:true},
				{type: "select", name: "单据状态", code: "documentStatusIds", placeholder: "请选择单据状态", dictCode:'DOCUMENT_STATUS',multiple:true},
			],
			columns: [
				// {code: "操作", name: "操作", width: "50",click:"show",fixedValue:this.$t('button.detail')},
				{code: "code", name: "单据编码", width:"130",sortable:true,click: "showDocument"},
				{code: "assetInfo.code", name: "资产编码", width:"135",click:"show1",sortable:true},
				{code: "createUser", name: "制单人", width:"80",formatter: "DICT",key: "SysUser"},
				{code: "createOrganizationId", name: "制单部门", width:"220",formatter: "DICT",key: "SysOrganization"},
				{code: "createTime", name: "制单时间", width:"110",sortable:true,formatter: "DATE"},
				{code: "assetInfo.name", name: "资产名称", width:"150"},
				{code: "assetInfo.brand", name: "品牌", width:"150"},
				{code: "assetInfo.model", name: "规格型号", width:"150"},
				{code: "assetInfo.serialCode", name: "序列号", width:"150"},
				{code: "assetInfo.assetCategoryId", name: "资产类别", width:"220",formatter: "DICT",key: "FamsAssetCategory",sortable:true},
				{code: "status", name: "单据状态", width:"85",sortable: true,formatter:"DICT",key: "DOCUMENT_STATUS"},
				{code: "assetInfo.assetCount", name: "数量", width:"",summary:true},
				{code: "assetInfo.originalValue", name: "资产原值", width:"150",summary:true},
				{code: "assetInfo.netValue", name: "资产净值", width:"150",summary:true},
				{code: "assetInfo.boughtDate", name: "购置日期", width:"110",formatter: "DATE"},
			],
			parentLevelCode:"",
			assetIds:[],
			manageDeptIds:tool.current.readDeptIds(),
			manageAssetCategoryIds:tool.current.manageAssetCategoryIds(),
			loading: false,
			loginUsers: tool.current.user(),
		}
	},
	created() {
	},
	mounted() {
	},
	beforeMount() {

	},
	methods: {
		buttonHandler(index, name) {
			this.$options.methods[name](this, index);
		},
		async searchTreeHandler(data) {
			let queryParams = {pathCode:data.levelCode}
			this.parentLevelCode= data.levelCode;
			await this.$refs.table.refresh(queryParams);
		},

		async searchHandler() {
			let queryParams = {pathCode: this.parentLevelCode}
			await this.$refs.table.refresh(queryParams);
		},
		columnClickHandler(name,data){
			this.$options.methods[name](this, data);
		},
		async show(_this, row) {
			try {
				_this.loading = true;
				let rsp = await _this.$API.asset.document.findById.post({id: row.documentId});
				if (rsp.code === 2000) {
					let document = rsp.data;
					let entity = {modelId: row.documentId, cardId: document.processType.showCardId, _editFlag: false}
					if(row.id){
						let res = await _this.$API.asset.documentDetail.findById.post({id: row.id});
						if (res.code === 2000) {
							_this.$nextTick(() => {
								console.log(row);
								_this.$refs.dataCard.open(Object.assign(entity, res.data));
							});
						}
					}
				}

			} finally {
				_this.loading = false;
			}
		},
		async show1(_this, row) {
			let assetCategory = await _this.$cache.data.loadAssetCategory(row.assetInfo.assetCategoryId);
			try {
				_this.loading = true;
				let entity = {cardId: assetCategory.assetCardId, _editFlag: false}
				_this.$nextTick(() => {
					_this.cardPreview = false;
					_this.$refs.dataCard.open(Object.assign(entity, row.assetInfo));
				});
			} finally {
				_this.loading = false;
			}
		},
		//查看
		showDocument(_this,row) {
			_this.$nextTick(() => {
				_this.$router.push({name:"documentPrepare",params:{fromRouteId:_this.$TOOL.crypto.MD5(_this.$route.path),configId:row.documentConfigId,id:row.documentId}});
			})
		},
		//导出
		async export(_this,index) {
      let attachParams = {};
      attachParams.exportType = "queryExport";
      let exportApi = _this.$API.asset.documentDetail.export;
      let exTableRef = _this.$refs.table;
      let exportBtnRef = _this.$refs.button;
      let exportBtnIndex = index;
      await BaseTemplate.export(_this, exportApi, attachParams, exportBtnRef, exportBtnIndex, exTableRef);
		},
	}
}

</script>
<style scoped>

</style>
